package com.fxp.project.jiuselu.web.business.v1.controller.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.business.v1.request.user.ReqAvatar;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.commission.entity.JslSaleCommission;
import com.fxp.project.jiuselu.web.models.commission.service.JslSaleCommissionService;
import com.fxp.project.jiuselu.web.models.order.service.IJslUserBalanceService;
import com.fxp.project.jiuselu.web.models.user.entity.JslSaleInfo;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.service.IJslSaleInfoService;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import com.fxp.project.jiuselu.web.models.userbank.entity.JslUserBankCardInfo;
import com.fxp.project.jiuselu.web.models.userbank.service.IJslUserBankCardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * wj:代理人申请佣金结算
 */
@RestController
@RequestMapping("/v1/sale/commission")
public class CommissionController extends BaseController {
    @Resource
    private JslSaleCommissionService jslSaleCommissionService;
    @Autowired
    private IJslUserBankCardInfoService jslUserBankCardInfoService;
    @Autowired
    private IJslSaleInfoService jslSaleInfoService;
    @Autowired
    private IJslUserService jslUserService;
    @Autowired
    private IJslUserBalanceService jslUserBalanceService;

    @PostMapping("save")
    @CrossOrigin
    public CommonResult save(@RequestBody JslSaleCommission jslSaleCommission, BindingResult result) {
        super.checkBindResult(result);
        JslUserBankCardInfo byId = jslUserBankCardInfoService.getById(jslSaleCommission.getBankId());
        if (super.getCurrentUserInfo().getLevel() == 1) {
            if (null != byId) {
                jslSaleCommission.setBankName(byId.getBankName());
                jslSaleCommission.setBanSubBranchName(byId.getBanSubBranchName());
                jslSaleCommission.setBankName(byId.getCardUserName());
                jslSaleCommission.setCardUserName(byId.getCardUserName());
                jslSaleCommission.setCardUserPhone(byId.getCardUserPhone());
                jslSaleCommission.setCardNum(byId.getCardNum());
                jslSaleCommission.setBankName(byId.getBankName());
                jslSaleCommission.setUserid(super.getCurrentUserInfo().getUserId());
                jslSaleCommission.setCreateTime(new Date());
                jslSaleCommission.setNickName(jslSaleInfoService.getOne(new QueryWrapper<JslSaleInfo>().lambda().eq(JslSaleInfo::getBindId, super.getCurrentUserInfo().getUserId())).getSaleName());
                List<JslSaleCommission> list = jslSaleCommissionService.list(new QueryWrapper<JslSaleCommission>().apply("DATE_FORMAT(create_time,'%Y-%m-%d')=DATE_FORMAT(NOW(),'%Y-%m-%d')"));
                jslSaleCommission.setStatementno(new SimpleDateFormat("YYYYmmdd").format(new Date())+new DecimalFormat("0000").format(list.size()<1?1:list.size()));
                if (byId.getUserId().equals(super.getCurrentUserInfo().getUserId())) {
                    boolean save = jslSaleCommissionService.save(jslSaleCommission);
                    if (save) {
                        BigDecimal amount = jslSaleCommission.getAmount();
                        JslUser user = BaseController.getUser();
                        assert user != null;
                        BigDecimal userMoney = user.getUserMoney();
                        //提现金额大于用户余额
                        if (jslUserService.getUserMoney(super.getCurrentUserInfo().getUserId()).compareTo(amount)==-1){
                            return CommonResult.failed("申请失败,请刷新");
                        }
                        user.setUserMoney(userMoney.subtract(amount));
                        //修改用户余额
                        jslUserService.updateById(user);
                        //添加余额明细
                        jslUserBalanceService.addBalance(3,new BigDecimal("0.00").subtract(amount),String.valueOf(jslSaleCommission.getId()),user.getUserId(),user.getUserId());

                        return CommonResult.success("申请成功");
                    } else return CommonResult.failed("申请失败");
                } else {
                    return CommonResult.failed("不可以选择别人银行卡");
                }
            } else {
                return CommonResult.failed("请选择银行卡");
            }
        } else return CommonResult.failed("不是代理人");
    }
    @PostMapping("allList")
    @CrossOrigin
    public CommonResult allList() {
        List<JslSaleCommission> list = jslSaleCommissionService.list(new QueryWrapper<JslSaleCommission>().lambda().eq(JslSaleCommission::getUserid, super.getCurrentUserInfo().getUserId()).apply("status!=1"));
        list.stream().forEach(u->{
            u.setUser(jslUserService.getOne(new QueryWrapper<JslUser>().eq("user_id", u.getUserid())));
        });
        return CommonResult.success(list);
   }

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("0");
        BigDecimal bigDecimal1 = new BigDecimal("500");

        System.out.println(bigDecimal.subtract(bigDecimal1));
    }

}
